package com.wangzha.business.ctrl;

import com.alibaba.fastjson.JSON;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.wangzha.common.response.R;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class VersionManagerController extends Controller {

    public void update() {
        // 项目发布名称
        Map<String, Object> map = new HashMap<>(2);
        // 版本号
        Record record = Db.findFirst("select * from version_update order by createTime desc limit 1");
        String reqUrl = getRequest().getScheme() + "://" + getRequest().getServerName() + ":" + getRequest().getServerPort() + "/download/script?name=";
        Map<Object, Object> downloadjson = new HashMap<>(10);

        if (record != null) {
            Map<String, Object> mapAll = record.getColumns();
            map.put("newversion", mapAll.get("version"));
            List<String> list = JSON.parseArray((String)mapAll.get("update_file"), String.class);
            if (list != null && list.size() > 0) {
                for (String js : list) {
                    if (StringUtils.isNotBlank(js)) {
                        downloadjson.put(js, reqUrl + js);
                    }
                }
            }
        }
        map.put("downloadjson", JSON.toJSONString(downloadjson));
        renderJson(R.data(map, "更新脚本"));

    }

}
